//
//  HTThumbnailView.m
//  HelpThePoor
//
//  Created by lidechen on 2018/4/26.
//  Copyright © 2018年 van7ish. All rights reserved.
//

#import "HTThumbnailView.h"
#import "HTThumbnailCell.h"

@interface HTThumbnailView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    
}
@end

@implementation HTThumbnailView
@synthesize m_collectionView;
-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(60, 80);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 10;
        
        m_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W,120) collectionViewLayout:layout];
        m_collectionView.backgroundColor = [UIColor whiteColor];
        m_collectionView.delegate = self;
        m_collectionView.dataSource = self;
        m_collectionView.scrollsToTop = NO;
        m_collectionView.showsVerticalScrollIndicator = NO;
        m_collectionView.showsHorizontalScrollIndicator = NO;
        [m_collectionView registerClass:[HTThumbnailCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:m_collectionView];
    }
    return self;
}

-(void)reloadView{
    [m_collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.pages;
}


- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifier=@"cell";
    HTThumbnailCell *cell=(HTThumbnailCell*)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    cell.index = indexPath.row;
    if (_selectIndex == indexPath.row) {
        cell.layer.borderColor = [UIColor greenColor].CGColor;
        cell.layer.borderWidth = 1;
    }
    else{
        cell.layer.borderColor = [UIColor clearColor].CGColor;
    }
    if (self.native) {
        [cell.imageV setImage:[UIImage imageWithContentsOfFile:[self.filePath stringByAppendingPathComponent:self.dataArr[indexPath.row]]] ];
    } else {
//        @autoreleasepool{
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%ld.jpg", KHostAddress, self.filePath, (long)(indexPath.row + 1)]] placeholderImage:ImageNamed(@"placeholdershuji")];
//        }
    }
    cell.numlabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate) {
        [self.delegate didselectIndex:indexPath.row];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout



@end
